//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package org.activiti.engine.impl.persistence.entity.data.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.activiti.engine.impl.EventSubscriptionQueryImpl;
import org.activiti.engine.impl.Page;
import org.activiti.engine.impl.cfg.ProcessEngineConfigurationImpl;
import org.activiti.engine.impl.persistence.CachedEntityMatcher;
import org.activiti.engine.impl.persistence.entity.CompensateEventSubscriptionEntity;
import org.activiti.engine.impl.persistence.entity.CompensateEventSubscriptionEntityImpl;
import org.activiti.engine.impl.persistence.entity.EventSubscriptionEntity;
import org.activiti.engine.impl.persistence.entity.EventSubscriptionEntityImpl;
import org.activiti.engine.impl.persistence.entity.MessageEventSubscriptionEntity;
import org.activiti.engine.impl.persistence.entity.MessageEventSubscriptionEntityImpl;
import org.activiti.engine.impl.persistence.entity.SignalEventSubscriptionEntity;
import org.activiti.engine.impl.persistence.entity.SignalEventSubscriptionEntityImpl;
import org.activiti.engine.impl.persistence.entity.data.AbstractDataManager;
import org.activiti.engine.impl.persistence.entity.data.EventSubscriptionDataManager;
import org.activiti.engine.impl.persistence.entity.data.impl.cachematcher.EventSubscriptionsByExecutionAndTypeMatcher;
import org.activiti.engine.impl.persistence.entity.data.impl.cachematcher.EventSubscriptionsByExecutionIdMatcher;
import org.activiti.engine.impl.persistence.entity.data.impl.cachematcher.EventSubscriptionsByNameMatcher;
import org.activiti.engine.impl.persistence.entity.data.impl.cachematcher.EventSubscriptionsByProcInstTypeAndActivityMatcher;
import org.activiti.engine.impl.persistence.entity.data.impl.cachematcher.MessageEventSubscriptionsByProcInstAndEventNameMatcher;
import org.activiti.engine.impl.persistence.entity.data.impl.cachematcher.SignalEventSubscriptionByEventNameMatcher;
import org.activiti.engine.impl.persistence.entity.data.impl.cachematcher.SignalEventSubscriptionByNameAndExecutionMatcher;
import org.activiti.engine.impl.persistence.entity.data.impl.cachematcher.SignalEventSubscriptionByProcInstAndEventNameMatcher;

public class MybatisEventSubscriptionDataManager extends AbstractDataManager<EventSubscriptionEntity> implements EventSubscriptionDataManager {
  private static List<Class<? extends EventSubscriptionEntity>> ENTITY_SUBCLASSES = new ArrayList();
  protected CachedEntityMatcher<EventSubscriptionEntity> eventSubscriptionsByNameMatcher = new EventSubscriptionsByNameMatcher();
  protected CachedEntityMatcher<EventSubscriptionEntity> eventSubscritionsByExecutionIdMatcher = new EventSubscriptionsByExecutionIdMatcher();
  protected CachedEntityMatcher<EventSubscriptionEntity> eventSubscriptionsByProcInstTypeAndActivityMatcher = new EventSubscriptionsByProcInstTypeAndActivityMatcher();
  protected CachedEntityMatcher<EventSubscriptionEntity> eventSubscriptionsByExecutionAndTypeMatcher = new EventSubscriptionsByExecutionAndTypeMatcher();
  protected CachedEntityMatcher<EventSubscriptionEntity> signalEventSubscriptionByNameAndExecutionMatcher = new SignalEventSubscriptionByNameAndExecutionMatcher();
  protected CachedEntityMatcher<EventSubscriptionEntity> signalEventSubscriptionByProcInstAndEventNameMatcher = new SignalEventSubscriptionByProcInstAndEventNameMatcher();
  protected CachedEntityMatcher<EventSubscriptionEntity> signalEventSubscriptionByEventNameMatcher = new SignalEventSubscriptionByEventNameMatcher();
  protected CachedEntityMatcher<EventSubscriptionEntity> messageEventSubscriptionsByProcInstAndEventNameMatcher = new MessageEventSubscriptionsByProcInstAndEventNameMatcher();

  public MybatisEventSubscriptionDataManager(ProcessEngineConfigurationImpl processEngineConfiguration) {
    super(processEngineConfiguration);
  }

  public Class<? extends EventSubscriptionEntity> getManagedEntityClass() {
    return EventSubscriptionEntityImpl.class;
  }

  public List<Class<? extends EventSubscriptionEntity>> getManagedEntitySubClasses() {
    return ENTITY_SUBCLASSES;
  }

  public EventSubscriptionEntity create() {
    throw new UnsupportedOperationException();
  }

  public CompensateEventSubscriptionEntity createCompensateEventSubscription() {
    return new CompensateEventSubscriptionEntityImpl();
  }

  public MessageEventSubscriptionEntity createMessageEventSubscription() {
    return new MessageEventSubscriptionEntityImpl();
  }

  public SignalEventSubscriptionEntity createSignalEventSubscription() {
    return new SignalEventSubscriptionEntityImpl();
  }

  public long findEventSubscriptionCountByQueryCriteria(EventSubscriptionQueryImpl eventSubscriptionQueryImpl) {
    String query = "selectEventSubscriptionCountByQueryCriteria";
    return (Long)this.getDbSqlSession().selectOne("selectEventSubscriptionCountByQueryCriteria", eventSubscriptionQueryImpl);
  }

  public List<EventSubscriptionEntity> findEventSubscriptionsByQueryCriteria(EventSubscriptionQueryImpl eventSubscriptionQueryImpl, Page page) {
    String query = "selectEventSubscriptionByQueryCriteria";
    return this.getDbSqlSession().selectList("selectEventSubscriptionByQueryCriteria", eventSubscriptionQueryImpl, page);
  }

  public List<MessageEventSubscriptionEntity> findMessageEventSubscriptionsByProcessInstanceAndEventName(String processInstanceId, String eventName) {
    Map<String, String> params = new HashMap();
    params.put("processInstanceId", processInstanceId);
    params.put("eventName", eventName);
    return this.toMessageEventSubscriptionEntityList(this.getList("selectMessageEventSubscriptionsByProcessInstanceAndEventName", params, this.messageEventSubscriptionsByProcInstAndEventNameMatcher, true));
  }

  public List<SignalEventSubscriptionEntity> findSignalEventSubscriptionsByEventName(String eventName, String tenantId) {
    String query = "selectSignalEventSubscriptionsByEventName";
    Map<String, String> params = new HashMap();
    params.put("eventName", eventName);
    if (tenantId != null && !tenantId.equals("")) {
      params.put("tenantId", tenantId);
    }

    List<EventSubscriptionEntity> result = this.getList("selectSignalEventSubscriptionsByEventName", params, this.signalEventSubscriptionByEventNameMatcher, true);
    return this.toSignalEventSubscriptionEntityList(result);
  }

  public List<SignalEventSubscriptionEntity> findSignalEventSubscriptionsByProcessInstanceAndEventName(String processInstanceId, String eventName) {
    String query = "selectSignalEventSubscriptionsByProcessInstanceAndEventName";
    Map<String, String> params = new HashMap();
    params.put("processInstanceId", processInstanceId);
    params.put("eventName", eventName);
    return this.toSignalEventSubscriptionEntityList(this.getList("selectSignalEventSubscriptionsByProcessInstanceAndEventName", params, this.signalEventSubscriptionByProcInstAndEventNameMatcher, true));
  }

  public List<SignalEventSubscriptionEntity> findSignalEventSubscriptionsByNameAndExecution(String name, String executionId) {
    Map<String, String> params = new HashMap();
    params.put("executionId", executionId);
    params.put("eventName", name);
    return this.toSignalEventSubscriptionEntityList(this.getList("selectSignalEventSubscriptionsByNameAndExecution", params, this.signalEventSubscriptionByNameAndExecutionMatcher, true));
  }

  public List<EventSubscriptionEntity> findEventSubscriptionsByExecutionAndType(String executionId, String type) {
    Map<String, String> params = new HashMap();
    params.put("executionId", executionId);
    params.put("eventType", type);
    return this.getList("selectEventSubscriptionsByExecutionAndType", params, this.eventSubscriptionsByExecutionAndTypeMatcher, true);
  }

  public List<EventSubscriptionEntity> findEventSubscriptionsByProcessInstanceAndActivityId(String processInstanceId, String activityId, String type) {
    Map<String, String> params = new HashMap();
    params.put("processInstanceId", processInstanceId);
    params.put("eventType", type);
    params.put("activityId", activityId);
    return this.getList("selectEventSubscriptionsByProcessInstanceTypeAndActivity", params, this.eventSubscriptionsByProcInstTypeAndActivityMatcher, true);
  }

  public List<EventSubscriptionEntity> findEventSubscriptionsByExecution(String executionId) {
    return this.getList("selectEventSubscriptionsByExecution", executionId, this.eventSubscritionsByExecutionIdMatcher, true);
  }

  public List<EventSubscriptionEntity> findEventSubscriptionsByTypeAndProcessDefinitionId(String type, String processDefinitionId, String tenantId) {
    String query = "selectEventSubscriptionsByTypeAndProcessDefinitionId";
    Map<String, String> params = new HashMap();
    if (type != null) {
      params.put("eventType", type);
    }

    params.put("processDefinitionId", processDefinitionId);
    if (tenantId != null && !tenantId.equals("")) {
      params.put("tenantId", tenantId);
    }

    return this.getDbSqlSession().selectList("selectEventSubscriptionsByTypeAndProcessDefinitionId", params);
  }

  public List<EventSubscriptionEntity> findEventSubscriptionsByName(String type, String eventName, String tenantId) {
    Map<String, String> params = new HashMap();
    params.put("eventType", type);
    params.put("eventName", eventName);
    if (tenantId != null && !tenantId.equals("")) {
      params.put("tenantId", tenantId);
    }

    return this.getList("selectEventSubscriptionsByName", params, this.eventSubscriptionsByNameMatcher, true);
  }

  public List<EventSubscriptionEntity> findEventSubscriptionsByNameAndExecution(String type, String eventName, String executionId) {
    String query = "selectEventSubscriptionsByNameAndExecution";
    Map<String, String> params = new HashMap();
    params.put("eventType", type);
    params.put("eventName", eventName);
    params.put("executionId", executionId);
    return this.getDbSqlSession().selectList("selectEventSubscriptionsByNameAndExecution", params);
  }

  public MessageEventSubscriptionEntity findMessageStartEventSubscriptionByName(String messageName, String tenantId) {
    Map<String, String> params = new HashMap();
    params.put("eventName", messageName);
    if (tenantId != null && !tenantId.equals("")) {
      params.put("tenantId", tenantId);
    }

    MessageEventSubscriptionEntity entity = (MessageEventSubscriptionEntity)this.getDbSqlSession().selectOne("selectMessageStartEventSubscriptionByName", params);
    return entity;
  }

  public void updateEventSubscriptionTenantId(String oldTenantId, String newTenantId) {
    Map<String, String> params = new HashMap();
    params.put("oldTenantId", oldTenantId);
    params.put("newTenantId", newTenantId);
    this.getDbSqlSession().update("updateTenantIdOfEventSubscriptions", params);
  }

  public void deleteEventSubscriptionsForProcessDefinition(String processDefinitionId) {
    this.getDbSqlSession().delete("deleteEventSubscriptionsForProcessDefinition", processDefinitionId, EventSubscriptionEntityImpl.class);
  }

  protected List<SignalEventSubscriptionEntity> toSignalEventSubscriptionEntityList(List<EventSubscriptionEntity> result) {
    List<SignalEventSubscriptionEntity> signalEventSubscriptionEntities = new ArrayList(result.size());
    Iterator var3 = result.iterator();

    while(var3.hasNext()) {
      EventSubscriptionEntity eventSubscriptionEntity = (EventSubscriptionEntity)var3.next();
      signalEventSubscriptionEntities.add((SignalEventSubscriptionEntity)eventSubscriptionEntity);
    }

    return signalEventSubscriptionEntities;
  }

  protected List<MessageEventSubscriptionEntity> toMessageEventSubscriptionEntityList(List<EventSubscriptionEntity> result) {
    List<MessageEventSubscriptionEntity> messageEventSubscriptionEntities = new ArrayList(result.size());
    Iterator var3 = result.iterator();

    while(var3.hasNext()) {
      EventSubscriptionEntity eventSubscriptionEntity = (EventSubscriptionEntity)var3.next();
      messageEventSubscriptionEntities.add((MessageEventSubscriptionEntity)eventSubscriptionEntity);
    }

    return messageEventSubscriptionEntities;
  }

  static {
    ENTITY_SUBCLASSES.add(MessageEventSubscriptionEntityImpl.class);
    ENTITY_SUBCLASSES.add(SignalEventSubscriptionEntityImpl.class);
    ENTITY_SUBCLASSES.add(CompensateEventSubscriptionEntityImpl.class);
  }
}
